package com.ruoyi.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @Description:
 * @Company: 北京卡拉卡尔股份技术有限公司
 * @Author: Darren Ho
 * @Date: Created on 2019/2/22
 * @Version: V1.0
 */
public final class JacksonUtil {

    private final Logger logger = LoggerFactory.getLogger(JacksonUtil.class);

    private final static ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    static {
        OBJECT_MAPPER.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    }

    private JacksonUtil() {
    }

    public static <T> T fromJson(String toString, Class<T> clazz) {
        T t;
        try {
            t = OBJECT_MAPPER.readValue(toString, clazz);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return t;
    }

    public static String toJson(Object object) {
        String result;
        try {
            result=  OBJECT_MAPPER.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}
